/**
 * 
 */
package com.nari.heatshare.action;

import java.io.File;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.json.JSONUtil;
import org.apache.struts2.json.annotations.JSON;

import com.nari.baseinfo.model.IBuilding;
import com.nari.baseinfo.model.ICommunity;
import com.nari.baseinfo.model.IConcentrator;
import com.nari.baseinfo.model.IHeatRoom;
import com.nari.baseinfo.model.IProject;
import com.nari.baseinfo.service.IBuildingService;
import com.nari.baseinfo.service.ICommunityService;
import com.nari.baseinfo.service.IConcentratorService;
import com.nari.baseinfo.service.IHeatRoomService;
import com.nari.baseinfo.service.IProjectService;
import com.nari.coreframe.action.struts2.JSONActionSupport;
import com.nari.heatshare.model.ITemperaturePoint;
import com.nari.heatshare.service.ITemperaturePointService;
import com.nari.heatshare.utils.excelupload.res.HeatShareExcelResource;
import com.nari.platform.model.PageCond;
import com.nari.platform.model.ResponseMsg;
import com.nari.platform.model.ResultWithPageCond;
import com.opensymphony.xwork2.Action;

/**
 * 
 * @author Xiaopeng
 * @date 2014-3-11
 */
public class TemperaturePointAction extends JSONActionSupport {
	private static final long serialVersionUID = -2871813417772181239L;
	private ITemperaturePointService iTemperaturePointService;
	private IProjectService iProjectService;
	private ICommunityService iCommunityService;
	private IBuildingService iBuildingService;
	private IHeatRoomService iHeatRoomService;
	private IConcentratorService iConcentratorService;
	//温度点信息查询
	private String projectUID;
	private String communityUID;
	private String buildingUID;
	private String concentratorUID;
	private String seriousNum;
	private List<IProject> projectList;
	private List<ICommunity> communityList;
	private List<IBuilding> buildingList;
	private List<IHeatRoom> roomList;
	private List<IConcentrator> concentratorList;
	//分页查询
	private int pageIndex = 1; //默认查询第一页列表
	private PageCond pageCond;
	private int pageSize;
	private ResultWithPageCond<ITemperaturePoint> tempPointPage;
	private Map<String, String> pageProjectMap; //所属项目id-projectName映射表
	private Map<String, String> pageCommunityMap; //所属小区id-communityName映射表
	private Map<String, String> pageBuildingMap; //所属楼栋id-buildingName映射表
	private Map<String, String> pageRoomMap; //所属热用户id-houseNo映射表
	//温度点信息管理
	private String tempPointUID;
	private String heatRoomUID;
	private ITemperaturePoint temperaturePoint;
	private List<IHeatRoom> heatRoomList;
	//基础资料导入
	private File attachFile;
	private String attachFileFileName;
	//查询集中器所属的温度点信息
	private List<ITemperaturePoint> tempPointList;
	
	/**
	 * 分页查询温度点信息
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-3-11
	 */
	public String queryTemperaturePointPage() throws Exception {
		//所属项目列表
		this.projectList = this.iProjectService.queryAllProjectInOrder();
		if(this.projectList == null) {
			this.projectList = new ArrayList<IProject>();
		}
		//所属小区列表
		if(this.projectUID!=null && !"".equals(projectUID)) {
			this.communityList = this.iCommunityService.queryCommunitysByBelongings(null, null, null, projectUID);
		} else {
			this.communityList = new ArrayList<ICommunity>();
		}
		//所属楼栋列表
		this.buildingList = this.iBuildingService.queryBuildingByBelongings(projectUID, communityUID);
		//集中器列表
		this.concentratorList = this.iConcentratorService.queryConcentratorsByBelongs(projectUID, communityUID, null);
		//分页信息
		this.pageCond = new PageCond();
		int start = (this.pageIndex - 1) * this.pageCond.getLimit();
		this.pageCond.setStart(start);
		//分页结果
		this.tempPointPage = this.iTemperaturePointService.queryTempPointPage(seriousNum, projectUID, communityUID, buildingUID, concentratorUID, pageCond);
		//计算结果集的页数
		this.pageSize = new BigDecimal(this.tempPointPage.getPageCond().getTotal())
				.divide(new BigDecimal(this.tempPointPage.getPageCond().getLimit()), RoundingMode.UP).intValue();
		if(pageSize == 0) pageSize = 1;
		// 项目映射表
		this.pageProjectMap = new HashMap<String, String>();
		for (IProject project : this.projectList) {     	
			this.pageProjectMap.put(project.getId(), project.getProjectName());
		}
		// 小区映射表
		this.pageCommunityMap = this.iTemperaturePointService.queryReferCommunitysByTpList(tempPointPage.getList());
		// 楼栋映射表
		this.pageBuildingMap = this.iTemperaturePointService.queryReferBuildingsByTpList(tempPointPage.getList());
		return "tempPointPage";
	}
	
	/**
	 * 温度点信息管理
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-3-11
	 */
	public String queryTempPointForManage() throws Exception {
		if(tempPointUID!=null && !"".equals(tempPointUID)) {
			this.temperaturePoint = this.iTemperaturePointService.queryEntityById(tempPointUID);
			//所属项目
			this.projectUID = this.temperaturePoint.getProjectID();
			//所属小区
			this.communityUID = this.temperaturePoint.getCommunityID();
			//所属楼栋
			this.buildingUID = this.temperaturePoint.getBuildingID();
			//所属热用户
			this.heatRoomUID = this.temperaturePoint.getRoomId();
		}
		//所属项目列表
		this.projectList = this.iProjectService.queryAllProjectInOrder();
		if(this.projectList == null) {
			this.projectList = new ArrayList<IProject>();
		}
		//所属小区列表
		if(this.projectUID!=null && !"".equals(projectUID)) {
			this.communityList = this.iCommunityService.queryCommunitysByBelongings(null, null, null, projectUID);
		} else {
			this.communityList = new ArrayList<ICommunity>();
		}
		//所属楼栋列表
		this.buildingList = this.iBuildingService.queryBuildingByBelongings(projectUID, communityUID);
		//所属热用户列表
		this.heatRoomList = this.iHeatRoomService.queryHeatRoomsByBelongs(projectUID, communityUID, null, buildingUID);
		//所属集中器列表
		this.concentratorList = this.iConcentratorService.queryConcentratorsByBelongs(projectUID, communityUID, null);
		return "tempPointManage";
	}
	
	/**
	 * 新增或修改温度点信息
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-3-12
	 */
	public String saveOrUpdateTempPoint() throws Exception {
		this.rmsg = iTemperaturePointService.saveOrUpdateTempPoint(temperaturePoint);
		this.writeJsonStrToResponse(JSONUtil.serialize(rmsg));
		return Action.SUCCESS;
	}
	
	/**
	 * 温度点信息删除
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-3-12
	 */
	public String deleteTempPointByUID() throws Exception {
		this.iTemperaturePointService.deleteEntityById(tempPointUID);
		this.rmsg = ResponseMsg.getInstance(true, "");
		this.writeJsonStrToResponse(JSONUtil.serialize(rmsg));
		return Action.SUCCESS;
	}
	
	/**
	 * 下载Excel导入模板
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-3-12
	 */
	public String downloadExcelTemplate() throws Exception {
		//设置文件输出流
		this.setTargetFile(HeatShareExcelResource.getTempPointResource());
		//设置模板名称
		this.setOutputFileName("温度点Excel导入模板.xls");
		return "downloadTemplate";
	}
	
	/**
	 * 温度点资料导入初始化
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-3-13
	 */
	public String initTempPointUpload() throws Exception {
		return "tempPointUploadInit";
	}
	
	/**
	 * 温度点Excel导入
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-3-13
	 */
	public String uploadTempPointBatch() throws Exception {
		if(attachFileFileName.toLowerCase().endsWith("xls") 
				|| attachFileFileName.toLowerCase().endsWith("xlsx")) {
			this.rmsg = iTemperaturePointService.saveOrUpdateTempPointBatch(attachFile, attachFileFileName);
		} else {
			this.rmsg = ResponseMsg.getInstance(false, "请上传正确的Excel数据文件！");
		}
		this.writeResponseToFileAsyncSubmit(JSONUtil.serialize(rmsg));
		return Action.SUCCESS;
	}
	
	/**
	 * 通过集中器信息查询所属的温度点信息
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-3-17
	 */
	public String queryTempPointListByConcentratorUID() throws Exception {
		//查询集中器所属温度点列表
		this.tempPointList = this.iTemperaturePointService.queryTempPointsByConcentratorUID(concentratorUID);
		//查询温度点对应的楼栋映射
		this.pageBuildingMap = this.iTemperaturePointService.queryReferBuildingsByTpList(tempPointList);
		return "conTempPointList";
	}
	
	/**
	 * 通过所属楼栋查询温度点列表
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-3-25
	 */
	public String queryTempPointListByReferBuilding() throws Exception {
		this.tempPointList = this.iTemperaturePointService.queryTempPointListByBelongs(null, null, buildingUID, null);
		return "buildingTpList";
	}

	/**
	 * @return the iTemperaturePointService
	 */
	@JSON(serialize=false)
	public ITemperaturePointService getiTemperaturePointService() {
		return iTemperaturePointService;
	}

	/**
	 * @param iTemperaturePointService the iTemperaturePointService to set
	 */
	public void setiTemperaturePointService(
			ITemperaturePointService iTemperaturePointService) {
		this.iTemperaturePointService = iTemperaturePointService;
	}

	/**
	 * @return the iProjectService
	 */
	@JSON(serialize=false)
	public IProjectService getiProjectService() {
		return iProjectService;
	}

	/**
	 * @param iProjectService the iProjectService to set
	 */
	public void setiProjectService(IProjectService iProjectService) {
		this.iProjectService = iProjectService;
	}

	/**
	 * @return the iCommunityService
	 */
	@JSON(serialize=false)
	public ICommunityService getiCommunityService() {
		return iCommunityService;
	}

	/**
	 * @param iCommunityService the iCommunityService to set
	 */
	public void setiCommunityService(ICommunityService iCommunityService) {
		this.iCommunityService = iCommunityService;
	}

	/**
	 * @return the iBuildingService
	 */
	@JSON(serialize=false)
	public IBuildingService getiBuildingService() {
		return iBuildingService;
	}

	/**
	 * @param iBuildingService the iBuildingService to set
	 */
	public void setiBuildingService(IBuildingService iBuildingService) {
		this.iBuildingService = iBuildingService;
	}

	/**
	 * @return the iHeatRoomService
	 */
	@JSON(serialize=false)
	public IHeatRoomService getiHeatRoomService() {
		return iHeatRoomService;
	}

	/**
	 * @param iHeatRoomService the iHeatRoomService to set
	 */
	public void setiHeatRoomService(IHeatRoomService iHeatRoomService) {
		this.iHeatRoomService = iHeatRoomService;
	}

	/**
	 * @return the iConcentratorService
	 */
	@JSON(serialize=false)
	public IConcentratorService getiConcentratorService() {
		return iConcentratorService;
	}

	/**
	 * @param iConcentratorService the iConcentratorService to set
	 */
	public void setiConcentratorService(IConcentratorService iConcentratorService) {
		this.iConcentratorService = iConcentratorService;
	}

	/**
	 * @return the projectUID
	 */
	public String getProjectUID() {
		return projectUID;
	}

	/**
	 * @param projectUID the projectUID to set
	 */
	public void setProjectUID(String projectUID) {
		this.projectUID = projectUID;
	}

	/**
	 * @return the communityUID
	 */
	public String getCommunityUID() {
		return communityUID;
	}

	/**
	 * @param communityUID the communityUID to set
	 */
	public void setCommunityUID(String communityUID) {
		this.communityUID = communityUID;
	}

	/**
	 * @return the buildingUID
	 */
	public String getBuildingUID() {
		return buildingUID;
	}

	/**
	 * @param buildingUID the buildingUID to set
	 */
	public void setBuildingUID(String buildingUID) {
		this.buildingUID = buildingUID;
	}

	/**
	 * @return the concentratorUID
	 */
	public String getConcentratorUID() {
		return concentratorUID;
	}

	/**
	 * @param concentratorUID the concentratorUID to set
	 */
	public void setConcentratorUID(String concentratorUID) {
		this.concentratorUID = concentratorUID;
	}

	/**
	 * @return the seriousNum
	 */
	public String getSeriousNum() {
		return seriousNum;
	}

	/**
	 * @param seriousNum the seriousNum to set
	 */
	public void setSeriousNum(String seriousNum) {
		this.seriousNum = seriousNum;
	}

	/**
	 * @return the projectList
	 */
	public List<IProject> getProjectList() {
		return projectList;
	}

	/**
	 * @param projectList the projectList to set
	 */
	public void setProjectList(List<IProject> projectList) {
		this.projectList = projectList;
	}

	/**
	 * @return the communityList
	 */
	public List<ICommunity> getCommunityList() {
		return communityList;
	}

	/**
	 * @param communityList the communityList to set
	 */
	public void setCommunityList(List<ICommunity> communityList) {
		this.communityList = communityList;
	}

	/**
	 * @return the buildingList
	 */
	public List<IBuilding> getBuildingList() {
		return buildingList;
	}

	/**
	 * @param buildingList the buildingList to set
	 */
	public void setBuildingList(List<IBuilding> buildingList) {
		this.buildingList = buildingList;
	}

	/**
	 * @return the roomList
	 */
	public List<IHeatRoom> getRoomList() {
		return roomList;
	}

	/**
	 * @param roomList the roomList to set
	 */
	public void setRoomList(List<IHeatRoom> roomList) {
		this.roomList = roomList;
	}

	/**
	 * @return the concentratorList
	 */
	public List<IConcentrator> getConcentratorList() {
		return concentratorList;
	}

	/**
	 * @param concentratorList the concentratorList to set
	 */
	public void setConcentratorList(List<IConcentrator> concentratorList) {
		this.concentratorList = concentratorList;
	}

	/**
	 * @return the pageIndex
	 */
	public int getPageIndex() {
		return pageIndex;
	}

	/**
	 * @param pageIndex the pageIndex to set
	 */
	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	/**
	 * @return the pageCond
	 */
	public PageCond getPageCond() {
		return pageCond;
	}

	/**
	 * @param pageCond the pageCond to set
	 */
	public void setPageCond(PageCond pageCond) {
		this.pageCond = pageCond;
	}

	/**
	 * @return the pageSize
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * @param pageSize the pageSize to set
	 */
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	/**
	 * @return the tempPointPage
	 */
	public ResultWithPageCond<ITemperaturePoint> getTempPointPage() {
		return tempPointPage;
	}

	/**
	 * @param tempPointPage the tempPointPage to set
	 */
	public void setTempPointPage(ResultWithPageCond<ITemperaturePoint> tempPointPage) {
		this.tempPointPage = tempPointPage;
	}

	/**
	 * @return the pageProjectMap
	 */
	public Map<String, String> getPageProjectMap() {
		return pageProjectMap;
	}

	/**
	 * @param pageProjectMap the pageProjectMap to set
	 */
	public void setPageProjectMap(Map<String, String> pageProjectMap) {
		this.pageProjectMap = pageProjectMap;
	}

	/**
	 * @return the pageCommunityMap
	 */
	public Map<String, String> getPageCommunityMap() {
		return pageCommunityMap;
	}

	/**
	 * @param pageCommunityMap the pageCommunityMap to set
	 */
	public void setPageCommunityMap(Map<String, String> pageCommunityMap) {
		this.pageCommunityMap = pageCommunityMap;
	}

	/**
	 * @return the pageBuildingMap
	 */
	public Map<String, String> getPageBuildingMap() {
		return pageBuildingMap;
	}

	/**
	 * @param pageBuildingMap the pageBuildingMap to set
	 */
	public void setPageBuildingMap(Map<String, String> pageBuildingMap) {
		this.pageBuildingMap = pageBuildingMap;
	}

	/**
	 * @return the pageRoomMap
	 */
	public Map<String, String> getPageRoomMap() {
		return pageRoomMap;
	}

	/**
	 * @param pageRoomMap the pageRoomMap to set
	 */
	public void setPageRoomMap(Map<String, String> pageRoomMap) {
		this.pageRoomMap = pageRoomMap;
	}

	/**
	 * @return the tempPointUID
	 */
	public String getTempPointUID() {
		return tempPointUID;
	}

	/**
	 * @param tempPointUID the tempPointUID to set
	 */
	public void setTempPointUID(String tempPointUID) {
		this.tempPointUID = tempPointUID;
	}

	/**
	 * @return the heatRoomUID
	 */
	public String getHeatRoomUID() {
		return heatRoomUID;
	}

	/**
	 * @param heatRoomUID the heatRoomUID to set
	 */
	public void setHeatRoomUID(String heatRoomUID) {
		this.heatRoomUID = heatRoomUID;
	}

	/**
	 * @return the temperaturePoint
	 */
	public ITemperaturePoint getTemperaturePoint() {
		return temperaturePoint;
	}

	/**
	 * @param temperaturePoint the temperaturePoint to set
	 */
	public void setTemperaturePoint(ITemperaturePoint temperaturePoint) {
		this.temperaturePoint = temperaturePoint;
	}

	/**
	 * @return the heatRoomList
	 */
	public List<IHeatRoom> getHeatRoomList() {
		return heatRoomList;
	}

	/**
	 * @param heatRoomList the heatRoomList to set
	 */
	public void setHeatRoomList(List<IHeatRoom> heatRoomList) {
		this.heatRoomList = heatRoomList;
	}

	/**
	 * @return the attachFile
	 */
	public File getAttachFile() {
		return attachFile;
	}

	/**
	 * @param attachFile the attachFile to set
	 */
	public void setAttachFile(File attachFile) {
		this.attachFile = attachFile;
	}

	/**
	 * @return the attachFileFileName
	 */
	public String getAttachFileFileName() {
		return attachFileFileName;
	}

	/**
	 * @param attachFileFileName the attachFileFileName to set
	 */
	public void setAttachFileFileName(String attachFileFileName) {
		this.attachFileFileName = attachFileFileName;
	}

	/**
	 * @return the tempPointList
	 */
	public List<ITemperaturePoint> getTempPointList() {
		return tempPointList;
	}

	/**
	 * @param tempPointList the tempPointList to set
	 */
	public void setTempPointList(List<ITemperaturePoint> tempPointList) {
		this.tempPointList = tempPointList;
	}
	
}
